package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.VerificationCodeBean;
import com.lc.luzhengtang.conn.ConnVerificationCode2;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/27.
 */

public class YanzhengActivity extends BaseActivity {
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.edit_phone)
    TextView editPhone;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.text_sendbtn)
    TextView textSendbtn;
    @BindView(R.id.layout_send_ben)
    LinearLayout layoutSendBen;
    @BindView(R.id.btn_register)
    Button btnRegister;

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                textSendbtn.setText(millisUntilFinished / 1000 + "s重新获取");

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {
                textSendbtn.setText("重新获取");
                textSendbtn.setEnabled(true);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yanzheng);
        ButterKnife.bind(this);
        textTitle.setText("原号码验证");
        editPhone.setText(getIntent().getStringExtra("phone"));
    }

    @OnClick({R.id.img_news, R.id.text_title, R.id.edit_phone, R.id.edit_code, R.id.text_sendbtn, R.id.layout_send_ben, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_title:
                break;
            case R.id.edit_phone:
                break;
            case R.id.edit_code:


                break;
            case R.id.text_sendbtn:
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(YanzhengActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editPhone.getText().toString().trim().length() < 11) {
                    LovelyToast.makeText(YanzhengActivity.this, "手机号不能小于11位", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(YanzhengActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    getVerificationCode();
                }
                break;
            case R.id.layout_send_ben:
                break;
            case R.id.btn_register:
                //跳转下一页
                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    LovelyToast.makeText(YanzhengActivity.this, "用户手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editPhone.getText().toString())) {
                    LovelyToast.makeText(YanzhengActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    // UtilToast.show(RegisterActivity.this, "请输入正确的手机号");
                } else if (!editCode.getText().toString().equals(myCode)) {
                    LovelyToast.makeText(YanzhengActivity.this, "验证码不正确", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    startActivity(new Intent(YanzhengActivity.this, ChangePhoneActivity2.class));
                    finish();
                }

                break;
        }
    }

    String myCode;

    public void getVerificationCode() {
        new ConnVerificationCode2(editPhone.getText().toString().trim(), "tong", BaseApplication.basePreferences.readUid(), new AsyCallBack<VerificationCodeBean>() {

            @Override
            public void onSuccess(String toast, int type, Object o, VerificationCodeBean registerBean) throws Exception {
                if (registerBean.getCode() == 200) {
                    countTime.start();
//                finish();myCode=registerBean.getData().getCode();
                    LovelyToast.makeText(YanzhengActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                    myCode = registerBean.getData().getCode() + "";
                } else {
                    LovelyToast.makeText(YanzhengActivity.this, registerBean.getMessage(), 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
               /* LovelyToast.makeText(RegisterActivity.this, ConnVerificationCode.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);*/

                textSendbtn.setEnabled(true);
                textSendbtn.setText("获取验证码");
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);

            }
        }


        ).execute(this);
    }

}
